package com.illumination.monitor.common.utils;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.illumination.monitor.common.dvo.OptionDVO;
import com.illumination.monitor.common.enums.CodeEnum;

/**
 * @author TangLingYun
 * @describe 枚举工具类
 */
public class EnumUtil {

	/** 通过code获取枚举 */
	public static <T extends CodeEnum<?>> T getEnumByCode(Serializable code, Class<T> enumClass) {
		for (T each : enumClass.getEnumConstants()) {
			if (code.equals(each.getCode())) {
				return each;
			}
		}
		return null;
	}
	
	/** 枚举转下拉列表 */
	public static <T extends CodeEnum<?>> List<OptionDVO> toOptions(Class<T> enumClass) {
		List<OptionDVO> list = new ArrayList<OptionDVO>();
		for (T each : enumClass.getEnumConstants()) {
			OptionDVO optionVO = OptionDVO.builder()
																	   .name(each.getMessage())
																	   .value(String.valueOf(each.getCode()))
																	   .build();
			list.add(optionVO);
		}
		return list;
	}
	
}